import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class Start {
	public static void main (String[] args){
		GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx      = 0;
        gbc.gridy      = 0;
        gbc.gridwidth  = 1;
        gbc.gridheight = 1;
        gbc.weightx    = 0.0;
        gbc.weighty    = 0.0;
        gbc.anchor     = GridBagConstraints.NORTHWEST;
        gbc.fill       = GridBagConstraints.NONE;
        gbc.insets     = new Insets(0, 1, 5, 0);
 
        
		final JFrame startFrame = new JFrame();
		startFrame.setLayout(new GridBagLayout());
		startFrame.setName("StartScreen");
		
		JPanel left = new JPanel();
		left.setLayout(new GridBagLayout());
		left.add(new JLabel("Number of Disks"), gbc);
		final JComboBox ns = new JComboBox();
		ns.addItem("3");
		ns.addItem("4");
		ns.addItem("5");
		ns.addItem("6");
		ns.addItem("7");
		ns.addItem("10");
		ns.addItem("13");
		ns.setEditable(true);
        gbc.gridy      = 1;
		left.add(ns, gbc);
		
		JPanel right = new JPanel();
		right.setLayout(new GridBagLayout());
        gbc.gridy      = 0;
        gbc.gridx      = 0;
		right.add(new JLabel("Start Bar"), gbc);
		gbc.gridx      = 1;		
		right.add(new JLabel("Goal Bar"), gbc);
        gbc.gridy	 = 1;

        gbc.gridx      = 0;
		final JComboBox ss = new JComboBox();
		ss.addItem("1");
		ss.addItem("2");
		ss.addItem("3");
        right.add(ss,gbc);
        
        final JComboBox	gs = new JComboBox();
		gs.addItem("3");      
		gs.addItem("2");
		gs.addItem("1");
		gbc.gridx	 = 1;
		right.add(gs,gbc);
        gbc.gridx      = 0;
        gbc.gridy	 = 2;
		right.add(new JLabel("Play With left/right Cursor Keys and Space"), gbc);
		
		final JButton start = new JButton("Start");
		gbc.fill = GridBagConstraints.VERTICAL;
		gbc.gridx = 3;
		gbc.gridy = 3;
		right.add(start);
		
        gbc.gridx      = 0;
        gbc.gridy      = 0;
        gbc.gridwidth  = 1;
        gbc.gridheight = 1;
        gbc.weightx    = 0.0;
        gbc.weighty    = 0.0;
        gbc.anchor     = GridBagConstraints.CENTER;
        gbc.fill       = GridBagConstraints.NONE;
        gbc.insets     = new Insets(0, 0, 5, 0);
		startFrame.add(left,gbc);
		gbc.gridx      = 1;
		startFrame.add(right,gbc);
		startFrame.pack();
		startFrame.setVisible(true);
		startFrame.setResizable(false);
		
		startFrame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent arg0) {
				System.exit(0);
			}
		});
		ns.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				boolean num;
		        JComboBox cb = (JComboBox)arg0.getSource();
		        String n = (String)cb.getSelectedItem();
		        num = check(n);
				if (!num)
					cb.setSelectedItem(cb.getItemAt(0));
			}
		});

		ss.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				if (ss.getSelectedItem().toString().equals(gs.getSelectedItem().toString()))
					start.setEnabled(false);
				else
					start.setEnabled(true);

			}
			
		});
		gs.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				if (gs.getSelectedItem().toString().equals(ss.getSelectedItem().toString()))
					start.setEnabled(false);
				else
					start.setEnabled(true);

				}

		}
		);

		start.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				RunGui(Integer.parseInt(ns.getSelectedItem().toString()), 
						Integer.parseInt(ss.getSelectedItem().toString()),
						Integer.parseInt(gs.getSelectedItem().toString()));
				startFrame.setVisible(false);
				
			}
		});
	}
	
	private static boolean check(String n){
		boolean num= true;
		for (int i=0; num && i < n.length(); i++){ 
			num &= Character.isDigit(n.charAt(i));
		}
		if (n.charAt(0) == '0' || n.charAt(0) == '1' || n.charAt(0) == '2')
			num = false;
		return num;
	}
	private static void RunGui(final int o, final int p, final int f){
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Gui(o,p,f);
            }}
        );
        }
	

	//private JComboBox ss;
	//private JComboBox gs;
}
